package com.tiancheng.trade.payment.service.impl;

import com.tiancheng.trade.payment.bo.cmq.TallyFlowMsgBO;
import com.tiancheng.trade.payment.model.PaymentOrder;
import com.tiancheng.trade.payment.model.PaymentRefundFlow;
import com.tiancheng.trade.payment.service.PaymentOrderService;
import com.tiancheng.trade.payment.service.PaymentRefundService;
import com.tiancheng.trade.payment.service.TallyService;
import com.tiancheng.trade.payment.mq.PaymentCenterMsgProducer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 推送给对账中心的处理Service接口实现
 *
 * @author kellen
 * @date 2024/6/1
 */
@Slf4j
@Service
public class TallyServiceImpl implements TallyService {

    @Resource
    private PaymentOrderService paymentOrderService;
    @Resource
    private PaymentRefundService paymentRefundService;
    @Resource
    private PaymentCenterMsgProducer paymentCenterMsgProducer;

    @Override
    public boolean payFlow(String flowId) {
        PaymentOrder paymentOrder = paymentOrderService.findByPaymentFlowId(flowId);

        TallyFlowMsgBO msgBO = new TallyFlowMsgBO();
        msgBO.setPaymentId(paymentOrder.getPaymentId());
        msgBO.setFlowId(flowId);
        msgBO.setPayChannelOrderid(paymentOrder.getTradeNo());
        msgBO.setTransactionType("pay");
        msgBO.setPayChannel(paymentOrder.getPaymentChannel());
        msgBO.setPayWay(paymentOrder.getPaymentWay());
        msgBO.setOrderAmount(paymentOrder.getOrderAmount());
        msgBO.setPayAmount(paymentOrder.getAmount());
        msgBO.setCreateTime(paymentOrder.getCreateTime());
        msgBO.setPayTime(paymentOrder.getFlowTime());
        msgBO.setUid(paymentOrder.getUserId());
        msgBO.setSlottingRate(paymentOrder.getSlottingRate());
        msgBO.setDiscount(paymentOrder.getDiscount());
        paymentCenterMsgProducer.tallyFlow(msgBO);
        return true;
    }

    @Override
    public boolean refundFlow(String flowId) {
        PaymentRefundFlow refundFlow = paymentRefundService.findByFlowId(flowId);
        PaymentOrder paymentOrder = paymentOrderService.findByPaymentFlowId(refundFlow.getPayFlowId());

        TallyFlowMsgBO msgBO = new TallyFlowMsgBO();
        msgBO.setPaymentId(refundFlow.getPaymentId());
        msgBO.setFlowId(flowId);
        msgBO.setPayChannelOrderid(refundFlow.getTradeNo());
        msgBO.setTransactionType("refund");
        msgBO.setPayChannel(refundFlow.getPaymentChannel());
        msgBO.setPayWay(refundFlow.getPaymentWay());
        msgBO.setOrderAmount(paymentOrder.getOrderAmount());
        msgBO.setPayAmount(paymentOrder.getAmount());
        msgBO.setRefundApplyAmount(refundFlow.getOrderAmount());
        msgBO.setRefundAmount(refundFlow.getAmount());
        msgBO.setCreateTime(refundFlow.getCreateTime());
        msgBO.setRefundTime(refundFlow.getFlowTime());
        msgBO.setUid(refundFlow.getUserId());
        msgBO.setOriginalFlowId(paymentOrder.getFlowId());
        msgBO.setOriginalPayChannelOrderid(paymentOrder.getTradeNo());
        msgBO.setSlottingRate(refundFlow.getSlottingRate());
        paymentCenterMsgProducer.tallyFlow(msgBO);
        return true;
    }
}
